package com.luying.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.luying.dao.StudentScoreDao;
import com.luying.pojo.*;
import com.luying.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentScoreDao studentScoreDao;
    @Override
    public PageResult queryIntelligenceScore(StudentScorePageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPageIndex(),pageQuery.getPageSize());
        List<ScoreList> list = studentScoreDao.selectIntelligenceScore(pageQuery);
        //计算绩点
        list.stream().forEach(scoreList -> {
            scoreList.setPoint(calculationPoint(scoreList.getScore()));
        });
        PageInfo<ScoreList> pageInfo = new PageInfo(list);
        PageResult pageResult = new PageResult(pageInfo.getTotal(),list);
        return pageResult;
    }

    @Override
    public List<BaseDown> queryYear() {
        return studentScoreDao.selectYear();
    }

    @Override
    public Object queryTotalScore(StudentScorePageQuery pageQuery) {
        List<TotalTest> totalTests = new ArrayList<>();
        //所有的成绩
        List<TotalScore> totalScores = studentScoreDao.selectTotalScore(pageQuery);
        int index=0;
        for(TotalScore totalScore:totalScores){
            index++;
            TotalTest totalTest = new TotalTest();
            List<ScoreList> list = totalScore.getList();
            double sum = 0;
            double totalCredit = 0;
            for(ScoreList scoreList:list){
                double point = calculationPoint(scoreList.getScore());
                sum+= point * scoreList.getCredit();
                totalCredit+=scoreList.getCredit();
            }
            double avg = sum/totalCredit;
            PointScore pointScore = studentScoreDao.selectPointScore(avg);
            totalTest.setId(index);
            totalTest.setYear(totalScore.getYear());
            totalTest.setTerm(totalScore.getTerm());
            totalTest.setTotalIntellectualScore(pointScore!=null&&pointScore.getScore()!=null?pointScore.getScore():59);
            totalTest.setTotalScore(pointScore.getScore());
            totalTests.add(totalTest);
        }
        List<TotalScore> totalMoral = studentScoreDao.selectTotalMoral(pageQuery);
        for(TotalScore totalScore:totalMoral){
            index++;
            List<ScoreList> list = totalScore.getList();
            double sum = 0;
            for(ScoreList scoreList:list){
                sum+= scoreList.getScore();
            }
            double t;
            t = sum>=35?35:sum;
            int sign = 0;
            for(TotalTest totalTest:totalTests){
                if(totalTest.getYear().equals(totalScore.getYear())&&totalTest.getTerm().intValue()==totalScore.getTerm().intValue()){
                    sign = 1;
                    totalTest.setTotalMoralScore(t);
                    totalTest.setTotalScore(totalTest.getTotalIntellectualScore()+t);
                }
            }
            if(sign==0){
                TotalTest totalTest = new TotalTest();
                totalTest.setId(index);
                totalTest.setYear(totalScore.getYear());
                totalTest.setTerm(totalScore.getTerm());
                totalTest.setTotalIntellectualScore(0);
                totalTest.setTotalMoralScore(t);
                totalTest.setTotalScore(t);
                totalTests.add(totalTest);
            }
        }
        return totalTests;
    }

    @Override
    public PageResult queryMoralScore(StudentScorePageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPageIndex(),pageQuery.getPageSize());
        List<ScoreList> list = studentScoreDao.selectMoralScore(pageQuery);
        PageInfo<ScoreList> pageInfo = new PageInfo(list);
        PageResult pageResult = new PageResult(pageInfo.getTotal(),list);
        return pageResult;

    }

    private double calculationPoint(double score){
        if(score<60){
            return 0;
        }else if(score>=60&&score<64){
            return 1;
        }else if(score>=64&&score<68){
            return 1.5;
        }else if(score>=68&&score<72){
            return 2;
        }else if(score>=72&&score<75){
            return 2.3;
        }else if(score>=75&&score<78){
            return 2.7;
        }else if(score>=78&&score<82){
            return 3;
        }else if(score>=82&&score<85){
            return 3.3;
        }else if(score>=85&&score<90){
            return 3.7;
        }
        return 4;
    }

}
